// 引入相关资源
import Star from "./elements/star";
import Circle from "./elements/circle";
import Wave from "./elements/wave";
import BackGround from "./runtime/background";
import Music from "./runtime/music";

// 初始化 画布
let ctx = canvas.getContext("2d");

// 初始化 云服务
wx.cloud.init();
const db = wx.cloud.database();

// 设置动画帧率
// wx.setPreferredFramesPerSecond(60);

// 初始化数据
var index = 0;
var voiceData = [];
var AvgData = [];
var loaded = false;
/**
 * 游戏主函数
 */
export default class Main {
  constructor() {
    // 维护当前requestAnimationFrame的id
    this.aniId = 0;
    this.avgData = this.avgData
    this.restart();
    this.login();
  }
  login() {
    // 获取 openid
    wx.cloud.callFunction({
      name: "login",
      success: res => {
        window.openid = res.result.openid;
        // console.log(window.openid);
        this.getAvgData();
      },
      fail: err => {
        console.error("get openid failed with error", err);
      }
    });
  }
  getVoiceData0() {
    // 避开数据大小限制的措施（分段获取）
    let dataId = [
      "5bce882fee4ea49e9176bb57",
      "5bce8845ee4ea49e9176bb7c",
      "5bce8855ee4ea49e9176bb96",
      "5bce886aee4ea49e9176bbad",
      "5bce887fee4ea49e9176bbd2",
      "5bce8894ee4ea49e9176bbee",
      "5bce88a4ee4ea49e9176bc01",
      "5bce88b4ee4ea49e9176bc28",
      "5bce88e9ee4ea49e9176bc49",
      "5bce88feee4ea49e9176bc6c"
    ];
    db.collection('voice').doc(dataId[0]).get().then(res => {
      voiceData.push(res.data['VD0']);
      db.collection('voice').doc(dataId[1]).get().then(res => {
        voiceData.push(res.data['VD1']);
        db.collection('voice').doc(dataId[2]).get().then(res => {
          voiceData.push(res.data['VD2']);
          db.collection('voice').doc(dataId[3]).get().then(res => {
            voiceData.push(res.data['VD3']);
            db.collection('voice').doc(dataId[4]).get().then(res => {
              voiceData.push(res.data['VD4']);
              db.collection('voice').doc(dataId[5]).get().then(res => {
                voiceData.push(res.data['VD5']);
                db.collection('voice').doc(dataId[6]).get().then(res => {
                  voiceData.push(res.data['VD6']);
                  db.collection('voice').doc(dataId[7]).get().then(res => {
                    voiceData.push(res.data['VD7']);
                    db.collection('voice').doc(dataId[8]).get().then(res => {
                      voiceData.push(res.data['VD8']);
                      db.collection('voice').doc(dataId[9]).get().then(res => {
                        voiceData.push(res.data['VD9']);
                        voiceData = voiceData[0].concat(voiceData[1]).concat(voiceData[2]).concat(voiceData[3]).concat(voiceData[4]).concat(voiceData[5]).concat(voiceData[6]).concat(voiceData[7]).concat(voiceData[8]).concat(voiceData[9]);
                        console.log(voiceData);
                        this.music.playBgm()
                        loaded = true;
                      })
                    })
                  })
                })
              })
            })
          })
        })
      })
    })

  }
  getAvgData() {
    db.collection('voice').doc('5bcfc7cfee4ea49e91772d0d').get().then(res => {
      AvgData = res.data['AVG']
      // console.log(AvgData);
      this.Star.getData(AvgData)
      this.Circle.getData(AvgData)
      this.Wave.getData(AvgData)
      this.music.playBgm()
      loaded = true;
    })
  }
  // 游戏重置
  restart() {

    this.bg = new BackGround(ctx);
    this.Star = new Star(ctx, AvgData);
    this.Circle = new Circle(ctx, AvgData);
    this.Wave = new Wave(ctx, AvgData);
    this.music = new Music();

    this.bindLoop = this.loop.bind(this);
    this.hasEventBind = false;

    // 清除动画
    window.cancelAnimationFrame(this.aniId);
    this.aniId = window.requestAnimationFrame(this.bindLoop, canvas);
  }

  /**
   * canvas重绘函数
   * 每一帧重新绘制所有的需要展示的元素
   */
  render() {
    // 渲染背景
    setInterval(() => {
      ctx.clearRect(0, 0, canvas.width, canvas.height)

      this.bg.render(ctx);
      if (loaded) {
        this.Star.render(ctx);
        this.Circle.render(ctx);
        this.Wave.render(ctx);
      }
    }, 15)
  }

  // 游戏逻辑更新主函数
  update() {
    // 背景更新
    this.bg.update();
  }

  // 实现游戏帧循环
  loop() {
    // databus.frame++
    this.render();
    // this.aniId = window.requestAnimationFrame(this.bindLoop, canvas);
  }
}